package com.sihai.ehr.web.action.contract;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.ContractVersion;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.ContractService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.List;

/**
 * @author: wxn
 * @since: 12-6-22 下午12:37
 *
 * 合同版本管理Action
 */

@Results({@Result(name = "version_list", location = "version_list.jsp"),
        @Result(name = "version_main", location = "contract_main.jsp"),
        @Result(name = "version_add", location = "version_add.jsp")})
public class VersionAction extends EHRAction{

    @BeanInject
    private ContractService contractService;

    @BeanInject
    private OrganizationService organizationService;

    private LimitedList<ContractVersion> versions;

    private ContractVersion version;

    private String ids; /*删除的版本ID列表(逗号分隔)*/

    private String operation; /*set:合同版本设置, audit:合同版本审核, list:合同版本浏览*/

    private List<Organization> organizations;

    @Action(value = "/contract/version")
    public String version() throws Exception {
        listAction = String.format("/contract/version_list.xhtml?operation=%s", operation);
        if(StringUtils.equals(operation, "set")){
           listTitle = "合同版本设置";
        }else if(StringUtils.equals(operation, "audit")){
            listTitle = "合同版本审核";
        }else if(StringUtils.equals(operation, "list")){
            listTitle = "合同版本浏览";
        }

        
        return "version_main";
    }

    //合同版本列表
    @Action(value = "/contract/version_list")
    @Override
    public String execute() throws Exception {
        ContractVersion v = new ContractVersion();
        v.setCancel(false);
        if(StringUtils.equals(operation, "set")){
            v.setAuditStatus(AuditStatus.wait_audit);
        }else if(StringUtils.equals(operation, "audit")){
           // v.setAuditStatus(AuditStatus.wait_audit);
        }else if(StringUtils.equals(operation, "list")){
            v.setCancel(null);
        }

        if(organizationId != null){
            Organization o = organizationService.get(organizationId);
            o.setId(organizationId);
            v.setOrganization(o);
        }

        //setCount(3);
        versions = contractService.findVersions(v, createLimit(), createSorter());

        /* 分页 */
        this.paging(versions);
        /* 设置翻页action */
        this.setPageUrl("contract/version_list.xhtml");

        return "version_list";
    }

    //跳转到合同版本添加页面
    @Action(value = "/contract/version_edit")
    public String edit() throws Exception {
        if(version != null && version.getId() != null){ //edit
            version = contractService.getVersion(version.getId());
            organizationId=version.getOrganization().getId();
        }
        organizations = organizationService.findAllChildren(
                        null, null);
        return "version_add";
    }

    //添加/修改合同版本
    @Action(value = "/contract/version_save")
    public String save() throws Exception {
        operation = "set";
        if(version == null){
            return version();
        }
                
        if(version.getId() != null){ //edit
            ContractVersion v = contractService.getVersion(version.getId());
            v.setContractName(version.getContractName());
            v.setContractVer(version.getContractVer());
            v.setCompanyName(version.getCompanyName());
            v.setContractTemplate(version.getContractTemplate());
            contractService.saveVersion(v);
        }else{//create
            version.setAuditStatus(AuditStatus.wait_audit);
            version.setCancel(false);
            contractService.saveVersion(version);
        }
        
        return version();
    }


    @Action(value = "/contract/version_remove")
    public String remove() throws Exception {
        if(StringUtils.isNotBlank(ids)){
            contractService.removeVersion(ids);
        }

        return execute();
    }


    public ContractVersion getVersion() {
        return version;
    }

    public void setVersion(ContractVersion version) {
        this.version = version;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public LimitedList<ContractVersion> getVersions() {
        return versions;
    }

    public void setVersions(LimitedList<ContractVersion> versions) {
        this.versions = versions;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }
}
